﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace SDTViewer
{
    public partial class FormMain : Form
    {
        private class FANDS
        {
            public string fname;
            public SDT sdt;
            public FANDS(string fname, SDT sdt)
            {
                this.fname = fname;
                this.sdt = sdt;
            }   
        }
        private SDT sdtData = null;
        private string fName = null;
        private bool edited = false;
        private bool Edited
        {
            get => edited; set
            {
                edited = value;
                lbEdited.Text = value ? "Edited" : "Saved";
            }
        }
        public FormMain()
        {
            InitializeComponent();
        }

        bool AskSave()
        {
            if (Edited)
            {
                var ret = MessageBox.Show("Save current file?", "Save or not", MessageBoxButtons.YesNoCancel, MessageBoxIcon.Question);
                switch (ret)
                {
                    case DialogResult.Yes:
                        Save(); break;
                    case DialogResult.Cancel:
                        return false;
                    default:
                        break;
                }
            }
            return true;
        }
        private void openToolStripMenuItem_Click(object sender, EventArgs e)
        {
            if (!AskSave()) return;
            if (ofd.ShowDialog() == DialogResult.OK)
            {
                dgv.DataSource = null;
                GC.Collect();
                fName = ofd.FileName;
                lbSta.Text = "Loading " + fName + " ...";
                this.Text = "SDT Viewer - " + fName;
                bw.RunWorkerAsync(fName);
            }
        }

        private void quitToolStripMenuItem_Click(object sender, EventArgs e)
        {
            if (!AskSave()) return;
            Close();
        }

        private void dgv_RowStateChanged(object sender, DataGridViewRowStateChangedEventArgs e)
        {
            e.Row.HeaderCell.Value = string.Format("{0}", e.Row.Index + 1);
        }

        private void saveToolStripMenuItem_Click(object sender, EventArgs e)
        {
            Save();
        }

        void Save()
        {
            if (sdtData == null)
            {
                MessageBox.Show("No file opened.", "Oops...", MessageBoxButtons.OK, MessageBoxIcon.Asterisk);
                return;
            }
            if (fName == null)
            {
                if (sfd.ShowDialog() == DialogResult.OK)
                {
                    fName = sfd.FileName;
                }
                else
                {
                    return;
                }
            }
            lbSta.Text = "Saving " + fName + " ...";
            Edited = false;
            bw2.RunWorkerAsync(new FANDS(fName, sdtData));
        }
        private void bw_DoWork(object sender, DoWorkEventArgs e)
        {
            string filename = (string)e.Argument;
            e.Result = SDT.LoadSDTFile(filename);
        }

        private void bw_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
        {
            sdtData = (SDT)e.Result;
            dgv.DataSource = sdtData.DataTable;
            lbDtype.Text = "Data Type: " + (sdtData.DType == SDT.DTYPE_I ? "Int32" : "Float32");
            lbSta.Text = "Resizing columns ...";
            Application.DoEvents();
            var cnt = dgv.Columns.Count;
            for (int i = 0; i < cnt; ++i)
            {
                dgv.Columns[i].AutoSizeMode = DataGridViewAutoSizeColumnMode.ColumnHeader;
                Application.DoEvents();
            }
            lbSta.Text = "Ready";
        }

        private void bw2_DoWork(object sender, DoWorkEventArgs e)
        {
            var arg = e.Argument as FANDS;
            arg.sdt.Save(arg.fname);
            lbSta.Text = "Ready";
        }

        private void saveAsToolStripMenuItem_Click(object sender, EventArgs e)
        {
            if (sdtData == null)
            {
                MessageBox.Show("No file opened.", "Oops...", MessageBoxButtons.OK, MessageBoxIcon.Asterisk);
                return;
            }
            if (sfd.ShowDialog() == DialogResult.OK)
            {
                fName = sfd.FileName;
            }
            else
            {
                return;
            }
            lbSta.Text = "Saving " + fName + " ...";
            Edited = false;
            bw2.RunWorkerAsync(new FANDS(fName, sdtData));
        }

        private void dgv_CellBeginEdit(object sender, DataGridViewCellCancelEventArgs e)
        {
            Edited = true;
        }
    }
}
